﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Web.Model;
using Portal.Exam.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class ListFinishedExamStudent
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

                string ExamRoomId = (string)WebEnvironments.Request(SiteId, "ExamRoomId");
                ExamRoomCls OExamRoom = new ExamRoomBll().CreateModel(SiteId, ExamRoomId);
                if (OExamRoom == null) throw new Exception("Tham số không hợp lệ!");
                string RetHtml =
                    " <script language=javascript> \r\n" +
                    "   function ClientReading()\r\n"+
                    "   {\r\n"+
                    "       SiteId='"+SiteId+"';\r\n"+
                    "       Keyword=document.getElementById('txtKeyword').value;\r\n"+
                    "       AjaxOut = Portal.Exam.Render.ListFinishedExamStudent.GetHtml(SiteId,'" + ExamRoomId + "',Keyword).value;\r\n" +
                    "       if(AjaxOut.Error)\r\n"+
                    "       {\r\n"+
                    "           alert(AjaxOut.InfoMessage);\r\n"+
                    "           return;\r\n"+
                    "       }\r\n"+
                    "       document.getElementById('divListFinishedExamStudentMain').innerHTML=AjaxOut.HtmlContent;\r\n"+
                    "   }\r\n"+
                    " </script>\r\n" +
                    " <input id=txtPageIndex type=hidden value=\"0\">\r\n" +
                    " <div class=DatabaseFunction>\r\n" +
                    "        <ul>\r\n" +
                    "            <li onclick=\"javascript:ClientReading();\"><img class=image src=\""+IconRoot+"/Refresh.png\"><div>Đọc lại</div></li>\r\n" +
                    "            <li onclick=\"javascript:window.open('ExamRoomStudent.aspx?ExamRoomId="+ExamRoomId+"','_self');\"><img class=image src=\""+IconRoot+"/back.png\"><div>Quay lại</div></li>\r\n" +
                    "        </ul>\r\n" +
                    "  </div>\r\n" +
                    "  <h3>Danh sách sinh viên trong phòng thi - " + OExamRoom.RoomNo + " / " + OExamRoom.ExamSubject + "</h3>\r\n" +
                    "  <div class=\"tabFunction\">Từ khóa: <input id=txtKeyword value=\"\"><input type=button class=button value=\"Lọc\" onclick=javascript:ClientReading();></div>\r\n" +
                    "  <div id=divProcessing></div>" +
                    "  <div id=divListFinishedExamStudentMain class=tabcontent>" + GetHtml(SiteId, ExamRoomId, "").HtmlContent + "</div>";
                OAjaxOut.HtmlContent=RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.InfoMessage = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut GetHtml(
            string SiteId,
            string ExamRoomId,
            string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                DataTable dtListFinishedExamStudent = new ExamRoomStudentBll().ReadingExamRoomStudentHasFinished(SiteId, ExamRoomId, Keyword);
                if (dtListFinishedExamStudent.Rows.Count == 0)
                {
                    OAjaxOut.HtmlContent =
                        "  <div class=CssNoData>Không có phòng thi nào thỏa mãn điều kiện lọc</div>";
                    return OAjaxOut;
                }
                string RetHtml =
                    "   <div class=PageTitle>Số sinh viên : " + dtListFinishedExamStudent.Rows.Count.ToString("#,##0") + "</div>" +
                    "   <div class=\"tabcontent\">\r\n" +
                    "   <ul>";

                for (int iIndex = 0; iIndex < dtListFinishedExamStudent.Rows.Count; iIndex++)
                {
                    string ExamRoomStudentId = (string)dtListFinishedExamStudent.Rows[iIndex]["ExamRoomStudentId"];
                    string StudentCode = (string)dtListFinishedExamStudent.Rows[iIndex]["StudentCode"];
                    string StudentName = (string)dtListFinishedExamStudent.Rows[iIndex]["StudentName"];
                    int Sex = (int)dtListFinishedExamStudent.Rows[iIndex]["Sex"];
                    string sDob="";
                    try
                    {
                        DateTime Dob = (DateTime)dtListFinishedExamStudent.Rows[iIndex]["Dob"];
                        sDob = Dob.ToString("dd/MM/yyyy");
                    }
                    catch { }
                    string Course = (string)dtListFinishedExamStudent.Rows[iIndex]["Course"];
                    string TrainingLevel = (string)dtListFinishedExamStudent.Rows[iIndex]["TrainingLevel"];
                    string Class = (string)dtListFinishedExamStudent.Rows[iIndex]["Class"];
                    int TotalPoint = (int)dtListFinishedExamStudent.Rows[iIndex]["TotalPoint"];
                    int DonePoint = (int)dtListFinishedExamStudent.Rows[iIndex]["DonePoint"];
                    DateTime StartExam = (DateTime)dtListFinishedExamStudent.Rows[iIndex]["StartExam"];
                    RetHtml +=
                    "       <li onclick=\"javascript:window.open('ViewResultExamStudent.aspx?ExamRoomStudentId="+ExamRoomStudentId+"','_self');\">" +
                    "           <div class=item>Sinh viên: " + StudentCode + " / "+StudentName+"</div>" +
                    "           <div class=item>Giới tính: " + (Sex==1?"Nam":"Nữ") + "</div>" +
                    "           <div class=item>Ngày sinh: " + sDob + "</td>" +
                    "           <div class=item>Khóa : " + Course + " /  Lớp: "+Class+" / Chuyên ngành: "+TrainingLevel+"</div>" +
                    "           <div class=item>Thi lúc: " + StartExam.ToString("dd/MM/yyyy HH:mm") + " - Điểm : " + DonePoint.ToString("#,##0") + " / " + TotalPoint.ToString("#,##0") + "</div>" +
                    "       </li>";
                }
                RetHtml +=
                    "   </ul>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = RetHtml;

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

    }
}
